package com.tanhua.dubbo.api;

import com.tanhua.dubbo.mappers.QuestionsMapper;
import com.tanhua.model.db.Questions;
import com.tanhua.model.vo.QuestionsVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class QuestionsApiImpl implements QuestionsApi {

    @Autowired
    private QuestionsMapper questionsMapper;

    @Override
    public Map<String, List<QuestionsVo>> findAll() {
        List<Questions> questions = questionsMapper.selectList(null);
        Map<String, List<QuestionsVo>> map = new HashMap();
        for (Questions question : questions) {
            QuestionsVo vo = QuestionsVo.inint(question);
            String questionnaireId = question.getQuestionnaireId().toString();
            if (map.containsKey(questionnaireId)){
                List<QuestionsVo> list = map.get(questionnaireId);
                list.add(vo);
                map.put(questionnaireId,list);
            }else {
                List<QuestionsVo> list2 = new ArrayList<>();
                list2.add(vo);
                map.put(questionnaireId,list2);
            }
        }
        return map;
    }
    //通过问题ids获取问题list
    @Override
    public List<Questions> findByIds(List<Long> questionIds) {
        List<Questions> questionsList = questionsMapper.selectBatchIds(questionIds);
        return questionsList;
    }
}
